import { ref } from 'vue'
import { getListStore } from '@/api/store'

/**
 * 仓库列表服务
 */
export default function () {
  const getStoreListLoading = ref(false)
  const getStoreListParam = ref<StorageAPIRequest.GetStoreListParam>({
    limit: 50,
    outStore: null,
    outStoreType: '',
    page: 1,
    storeSearch: '',
    userSearch: '',
  })
  const storeList = ref<Array<WarehouseAPIResponse.StoreListItem>>([])
  const total = ref(0)

  const resetGetStoreListParam = () => {
    getStoreListParam.value = {
      limit: 50,
      outStore: null,
      outStoreType: '',
      page: 1,
      storeSearch: '',
      userSearch: '',
    }
  }

  const getStoreList = async () => {
    try {
      getStoreListLoading.value = true
      const res = await getListStore(getStoreListParam.value)
      getStoreListLoading.value = false
      if (res.data.code === 20000) {
        storeList.value = res.data.data.rows
        total.value = Number(res.data.data.total)
        return ''
      }

      return res.data.message
    } catch (e) {
      getStoreListLoading.value = false
      return '获取仓库列表失败，请稍后重试'
    }
  }

  return {
    total,
    storeList,
    getStoreListParam,
    getStoreListLoading,
    getStoreList,
    resetGetStoreListParam,
  }
}
